package tsing.javase.thread.futher;

import java.util.Random;
import java.util.concurrent.*;

/**
 * 线程池的submit方法返回Future对象
 * 具体过程：向线程池提交任务，提交时线程池会立刻返回一个空的Future容器。当线程的任务执行完毕，可以获取结果时，线程池便将结果填入那个Future容器中
 */

public class OneFuture {
    public static void main(String[] args) {

        ExecutorService service = Executors.newFixedThreadPool(10);
        Future<Integer> future = service.submit(new CallableTask());

        try {
            System.out.println(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        service.shutdown();
    }

    static class CallableTask implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            Thread.sleep(3000);
            return new Random().nextInt();
        }
    }
}
